<template>
  <d-scroll-view  @refresherrefresh="getDetail" :refresher-triggered="triggered">
    <proj-detail-layout :process-instance-id="processInstanceId" :project-number="projectNumber">
    <d-forms label-width="200rpx">
      <d-forms-item label="申请人" :input="form.applicantName"></d-forms-item>
      <d-forms-item label="开票阶段" :type="DFormInputType.DICT" :dict-type="PM_DICT_TYPE.INVOICE_STAGE" :input="form.invoiceStage"></d-forms-item>
      <view v-if="hasDoc">
        <view class="form-title-1">相应报告</view>
        <d-card>
          <pm-file-upload readonly :files="form.doc"></pm-file-upload>
        </d-card>
      </view>
      <d-forms-item label="客户" :input="form.customerName"></d-forms-item>
      <d-forms-item label="纳税人识别号" :input="form.taxpayerCode"></d-forms-item>
      <d-forms-item label="开票地址" :input="form.invoiceAddress"></d-forms-item>
      <d-forms-item label="电话" :input="form.phone"></d-forms-item>
      <d-forms-item label="开户银行" :input="form.accountsBank"></d-forms-item>
      <d-forms-item label="开户行帐号" :input="form.account"></d-forms-item>
      <d-forms-item label="开票票种" :type="DFormInputType.DICT" :dict-type="PM_DICT_TYPE.INVOICE_TYPE" :input="form.invoiceType"></d-forms-item>
      <d-forms-item label="开票金额" :type="DFormInputType.AMOUNT" :input="form.invoiceAmount"></d-forms-item>
      <d-forms-item label="税点" :input="form.taxRate"></d-forms-item>
      <d-forms-item label="开票内容" :input="form.content"></d-forms-item>
      <d-forms-item label="已开票金额" :type="DFormInputType.AMOUNT" :input="form.invoicedAmount"></d-forms-item>
      <d-forms-item label="剩余开票金额" :type="DFormInputType.AMOUNT" :input="form.residueAmount"></d-forms-item>
      <d-forms-item label="含税合同金额" :type="DFormInputType.AMOUNT" :input="form.amountIncludingTax"></d-forms-item>
      <view>
        <view class="form-title-1">合同附件</view>
        <d-card>
          <pm-file-upload readonly :files="form.contractAttach"></pm-file-upload>
        </d-card>
      </view>
      <view>
        <view class="form-title-1">收票情况</view>
        <d-card v-for="(item, idx) in receiptStatusList" class="mb10" :key="'receiptStatus_'+idx">
          <view>
            <view class="form-title-2">{{ item.label }}</view>
            <d-forms-item disabled label-width="100rpx" label="备注" :input="item.content"></d-forms-item>
          </view>
        </d-card>
      </view>
      <d-forms-item label="开票方" :input="form.invoicePartyName"></d-forms-item>
      <d-forms-item label="补充说明" :input="form.supplement"></d-forms-item>
    </d-forms>
  </proj-detail-layout>
  </d-scroll-view>
</template>

<script setup lang="ts">
import ProjDetailLayout from "@/components/ProjDetailLayout/index.vue";
import {useInvoiceApi} from "@/api/pm/project/invoice";
import {PM_DICT_TYPE} from "@/utils/pmDict";
import {DFormInputType, InvoiceStageEnum} from "@/utils/enum";

defineOptions({ name: 'InvoiceApply' })

const initForm = {
  projectNumber: null,
  invoiceStage: null,
  invoiceStageName: null,
  customerId: null,
  customerName: null,
  taxpayerCode: null,
  invoiceAddress: null,
  phone: null,
  accountsBank: null,
  account: null,
  invoiceType: null,
  invoiceTypeName: null,
  invoiceAmount: null,
  taxRate: null,
  content: null,
  receiptsSituation: null,
  receiptsSituationSupplement: null,
  supplement: null,
  invoiceParty: null,
  invoicePartyName: null,
  storaged: null,
  id: null,
  applicant: null,
  applicantName: null,
  currentNode: null,
  invoiceFile: null,
  processInstanceId: null,
  result: null,
  createTime: null,
  doc: null,
  amountIncludingTax: null,
  contractAttach: null,
  invoicedAmount: null,
  projectName: null,
  projectType: null,
  residueAmount: null,
}
const form = ref<InvoiceApplyForm>(initForm);
const triggered = ref<boolean>(false);
function getDetail() {
  if (processInstanceId.value) {
    triggered.value = true;
    useInvoiceApi().getIAByProcessInstanceId(processInstanceId.value).then(res => {
      if (res.code === 0) {
        form.value = res.data || initForm;
      }
    }).finally(() => {
      triggered.value = false;
    })
  }
}

const receiptStatusList = computed(() => {
  if (form.value.receiptsSituationSupplement) {
    try {
      return JSON.parse(form.value.receiptsSituationSupplement);
    } catch (e) {
      return [];
    }
  }
  return [];
})

const hasDoc = computed(() => {
  const newForm = toRaw(form.value)
  return newForm.invoiceStage === InvoiceStageEnum.END_TEST || newForm.invoiceStage === InvoiceStageEnum.FIRST_TEST;
})

const projectNumber = ref<string>(null);
const processInstanceId = ref<string>(null);
onLoad((query) => {
  projectNumber.value = query['project_number']
  processInstanceId.value = query['process_instance_id']
  getDetail();
})
</script>

<style scoped lang="scss">

</style>
